package com.jk.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jk.controller.BaseController;
import com.jk.pojo.RecruitDelivery;
import com.jk.pojo.Executives;
import com.jk.pojo.Partner;
import com.jk.pojo.Project;
import com.jk.pojo.Recruit;
import com.jk.pojo.User;
import com.jk.service.ProjectService;
import com.jk.service.RecruitDeliveryService;
import com.jk.service.PartnerService;
import com.jk.service.UserService;

/**
 * 合伙人模块
 *
 * @author cuiP
 *
 */
@Controller
@RequestMapping("/admin/partner/")
public class PartnerController extends BaseController {

	private static final String JSP_PATH = "/partner/";

	@Resource
	private PartnerService partnerService;

	@Resource
	private RecruitDeliveryService recruitDeliverService;

	@Resource
	ProjectService projectService;

	@Resource
	private UserService userService;

	/**
	 * 合伙人列表
	 * @param request
	 * @param type
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("list.do")
	public String list(HttpServletRequest request,ModelMap modelMap){
		List<Partner> partnerList = partnerService.queryAll();

		modelMap.put("varList", partnerList);
		return JSP_PATH+"partner-list";
	}

	/**投递历史查询
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("historyList/{id}.do")
	public String queryHistoryList(HttpServletRequest request,ModelMap modelMap, @PathVariable("id") Long userId) {
		List<Project> varProjectList = projectService.queryProjectDelievery(request, modelMap, userId);
		List<Recruit> varRecruitList = recruitDeliverService.queryRecruitDelievery(request, modelMap, userId);

		if(modelMap != null){
			modelMap.addAttribute("varProjectList", varProjectList);
			modelMap.addAttribute("varRecruitList", varRecruitList);
		}
		return JSP_PATH+"partner-history";
	}

	/**合伙人详情
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("view/{id}.do")
	public String view(HttpServletRequest request,ModelMap modelMap, @PathVariable("id") Long id) {
		try {
			Partner partner = partnerService.queryById(id);
			//modelMap.clear();
			modelMap.addAttribute("partner",partner);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSP_PATH+"partner-view";
	}

	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delete/{id}.do",method=RequestMethod.DELETE)
	public Map<String, Object> delete(HttpServletRequest request,@PathVariable("id") Long id){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			User record = userService.queryById(id);
			if(record == null){
				messageMap.put("status", FAILURE);
				messageMap.put("message","用户不存在!");
				return messageMap;
			}

			userService.deleteById(id);
			messageMap.put("status", SUCCESS);
			messageMap.put("message",SUCCESS_DELETE_MESSAGE);
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","删除失败!");
		}
		return messageMap;
	}

	/**
	 * 启用||停用
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update_status/{id}.do",method=RequestMethod.PUT)
	public Map<String, Object> update_status(HttpServletRequest request,@PathVariable("id") Long id){
		Map<String, Object> messageMap = new HashMap<String,Object>();
		try {
			User record = userService.queryById(id);
			if(record == null){
				messageMap.put("status", FAILURE);
				messageMap.put("message","用户不存在!");
				return messageMap;
			}

			record.setStatus(!record.getStatus());
			userService.update(record);

			messageMap.put("status", SUCCESS);
			messageMap.put("message","成功!");
		} catch (Exception e) {
			//e.printStackTrace();
			messageMap.put("status", FAILURE);
			messageMap.put("message","删除失败!");
		}
		return messageMap;
	}
}
